package its.opt.graphs;

public class AcyclicOrientedGraph extends OrientedGraph {

	public AcyclicOrientedGraph(int n) {
		super(n);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see its.opt.graphs.OrientedGraph#addEdge(int, int)
	 */
	@Override
	public int addEdge(int i, int j) {
		if (canAddEdge(i, j))
			return super.addEdge(i, j);
		else
			return 0;
	}

	public boolean canAddEdge(int i, int j) {
		return (!existsPath(j, i)) && (!connected(i, j));
	}

	@Override
	public boolean canReverseEdge(int i, int j) {
		boolean result = false;

		if (connected(i, j)) {
			removeEdge(i, j);
			result = !existsPath(i, j);
			addEdge(i, j);
		}

		return result;
	}

	@Override
	public int reverseEdge(int i, int j) {
		if (connected(i, j)) {
			removeEdge(i, j);

			if (addEdge(j, i) != 0)
				return 1;
			else {
				addEdge(i, j);
				return 0;
			}
		} else
			return 0;
	}
}
